package com.deloitte.payrollmgtsys.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.google.appengine.api.rdbms.AppEngineDriver;

public class TaskDAO {

	public void addTask(Task task) throws SQLException {
		Connection connection = null;
		PreparedStatement ps = null;
		try {
			DriverManager.registerDriver(new AppEngineDriver());
			connection = DriverManager
					.getConnection("jdbc:google:rdbms://activitiwfsql:activitiwfdb/activitiwfdb");
			String statement = "insert into user_task (t_id, t_name, t_process_id, t_assignee, t_create_time, t_desc, "
					+ "t_due_date, t_priority, t_complete_user, t_complete_time, t_form_data) "
					+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(), ?)";
			ps = connection.prepareStatement(statement);
			ps.setInt(1, task.getId());
			ps.setString(2, task.getName());
			ps.setInt(3, task.getProcessID());
			ps.setString(4, task.getAssignee());
			ps.setTimestamp(5, task.getCreateTime());
			ps.setString(6, task.getDescription());
			ps.setTimestamp(7, task.getDueDate());
			ps.setInt(8, task.getPriority());
			ps.setString(9, task.getCompleteUser());
			ps.setString(10, task.getFormData());

			ps.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
